#pragma once
#ifndef _D3D_MESH_
#define _D3D_MESH_

#include <windows.h>
#include <d3dx9.h>

#include "..\common\def.h"
#include "..\common\arraylist.hpp"
#include "..\math\matrix.h"
#include "..\math\sphere.h"

#include "interface_types.h"
//#include "..\d3d_util.h"
#include "d3d_util.h"

struct D3DModel;
class CD3D9Device; 
struct Texture; 
struct EE_EXPORT D3DMesh : public D3DRenderable
{
	struct RenderInstance
	{
		RenderInstance(); 
		RenderInstance(D3DMesh *pMesh, const Matrix4f& transformation, float frame); 
		RenderInstance operator = (const RenderInstance& v); 

		const D3DMesh *m_pMesh; 
		float m_Frame; 
		Matrix4f	m_Transformation; 
	}; 
 
	int m_numVerts; 
	int m_numFrames; 
	unsigned int m_vertArrayBaseIdx; 

	 
	Texture* m_pTextureUnitB; 
	Texture* m_pTextureUnitC; 
	Texture* m_pTextureMask;  
	Texture* m_pTextureGI; 

	int m_Identity; 
	Texture* getMaskTexture();
	Texture* getTexture(const unsigned int idx);
	void setTexture(Texture* pTexture, const unsigned int unit); 
	HRESULT Create(const MeshPartData& data);
	HRESULT	Release(void); 
	HRESULT Reload(); 
	void Render(const unsigned int vbOffset);
	string GetType() const; 
	D3DMesh(CD3D9Device* const pDevice); 
	virtual~D3DMesh(void); 
}; 

struct EE_EXPORT D3DModelLOD : IResource
{
	friend CD3D9Device; 
	friend D3DModel; 
private:
	D3DModelLOD(CD3D9Device* const pDevice); 
	~D3DModelLOD(); 
protected:

	D3DMesh** m_pMeshs; 
	unsigned int m_meshCount;

public:
	D3DModelLOD(CD3D9Device* const pDevice, D3DMesh** pMeshData, unsigned int meshCount); 

	string GetType() const; 
	D3DMesh* getMesh(unsigned int index); 
	unsigned int getMeshCount() const; 
	void setRenderFlags(const int flags); 

	void Draw(const Matrix4f &transform, D3DShader * const m_pShader, const float frame); 
	void Draw(const Matrix4f &transform, const float frame); 
	HRESULT	Reload(void); 
	HRESULT Release(); 
}; 

struct EE_EXPORT D3DModel : IResource
{
	friend CD3D9Device; 
private:
	D3DModel(CD3D9Device* const pDevice); 
	~D3DModel(); 
	int m_RenderFlags;
protected:

	D3DModelLOD** m_pLODs; 
	unsigned int m_LODCount; 
	Spheref m_Bounds;

public:
	D3DModel(CD3D9Device* const pDevice, D3DModelLOD** pLODs, unsigned int LODCount); 
	string GetType() const; 
	D3DModelLOD* getLOD(unsigned int index); 
	unsigned int getLODCount() const; 
	void setRenderFlags(const int flags); 
	//float GetRadius() const;
	Spheref GetBounds() const;

	void SetIsInstanced(bool value);
	void Draw(const Matrix4f &transform, D3DShader * const m_pShader, const Vector3f& velocity); 
	void Draw(const Matrix4f &transform, const Vector3f& velocity); 
	void Draw(const Vector3f& velocity); 
	HRESULT	Reload(void); 
	HRESULT Release(); 

	void CaclulateBoundSphere();
}; 

#endif